import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import axios from 'axios'

Vue.config.productionTip = false

// 配置axios
axios.defaults.baseURL = 'http://localhost:8080'
Vue.prototype.$http = axios

// 使用ElementUI
Vue.use(ElementUI)

// 路由守卫
router.beforeEach((to, from, next) => {
  // 检查是否需要登录
  const publicPages = ['/admin/login', '/admin/register', '/home', '/test', '/result', '/mbti-result']
  const authRequired = !publicPages.includes(to.path)
  const loggedIn = localStorage.getItem('user')

  if (authRequired && !loggedIn) {
    return next('/admin/login')
  }
  
  next()
})

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
